// 根据请求host判断环境
const getServers = (req) => {
  // 判断是否是生产环境的请求
  const isProd = req && req.headers && req.headers.host === "8.137.62.152:8090";

  return isProd
    ? [
        {
          url: "http://8.137.62.152:8090",
          description: "生产环境",
        },
        {
          url: "http://localhost:8090",
          description: "开发环境",
        },
      ]
    : [
        {
          url: "http://localhost:8090",
          description: "开发环境",
        },
        {
          url: "http://8.137.62.152:8090",
          description: "生产环境",
        },
      ];
};

const options = {
  definition: {
    openapi: "3.0.0",
    info: {
      title: "Koa2 API 文档",
      version: "1.0.0",
      description: "API接口文档",
    },
    // 初始化时先设置默认顺序
    servers: getServers(),
    components: {
      securitySchemes: {
        Authorization: {
          type: "apiKey",
          in: "header",
          name: "Authorization",
          description: "请在此输入token",
        },
      },
    },
  },
  // 重要：这里指定要生成文档的路由文件
  apis: ["./routes/*.js"],
};

// 导出配置和获取服务器列表的函数
module.exports = {
  options,
  getServers,
};
